package 早晨练习0422

object Scala_04 {
  def main(args: Array[String]): Unit = {
    import PointOrder.pointOrder2
//    import PointOrder.pointOrder
    // 隐式转换，point => PointOrder2
    println(Point(1, 2) < Point(2, 2))
  }
}

// 3、⽐较java.awt.Point类的对象，按词典顺序⽐较（即依次⽐较x坐标和y坐标的值）
// 4、继续前⼀个练习，根据两个点到原点的距离进⾏⽐较。你如何在两种排序之间切换？
case class Point(var x: Int, var y: Int)

class PointOrder(point: Point) extends Ordered[Point] {
  override def compare(that: Point): Int = {
    if (point.x == that.x) {
      if (point.y == that.y) 0
      else if (point.y < that.y) -1
      else 1
    } else if(point.x > that.x) 1
    else -1
  }
}

class PointOrder2(point: Point) extends Ordered[Point] {
  override def compare(that: Point): Int = (point.x * point.x + point.y * point.y) - (that.x * that.x + that.y * that.y)
}

object PointOrder {
  implicit val pointOrder: Point => PointOrder = point => new PointOrder(point)
  implicit val pointOrder2: Point => PointOrder2 = point => new PointOrder2(point)
}
